package com.shop.product.service;


import com.shop.common.enums.ProductStatus;
import com.shop.product.model.ProductInfo;
import com.shop.product.repository.ProductInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ProductInfoServiceImpl implements ProductService{
    @Autowired
    private ProductInfoRepository productInfoRepository;
    public ProductInfo getProductInfo(String productId) {
        ProductInfo productInfo = productInfoRepository.findById(productId).orElse(null);
        log.info("productId:{} {}", productId,productInfo);
        return productInfo;
    }

    //推荐商品
    @Override
    public List<ProductInfo> ProductStatusAndRecommend(Integer pageNum, Integer pageSize) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return productInfoRepository.findAllByProductStatusAndRecommend(ProductStatus.BELOW,1,pageable);
    }
}
